#!/usr/bin/perl
use strict;

{
	open (TEST, ">test.data") or die;
	open (CORRECT_AB, ">correct_ab.data") or die;
	open (CORRECT_A, ">correct_a.data") or die;
	open (CORRECT_B, ">correct_b.data") or die;
	my $cnt_seq = 0;
	for (0..199)
	{
		my @ids;
		push(@ids, "ID_" . substr("AB", int(rand(2)), 1) . int(rand(500)));
		push(@ids, "ID_" . substr("AB", int(rand(2)), 1) . int(rand(500)));
	
		my $data_str = "Saved longline of something";
		if (int(rand(2)))
		{
			print TEST "IGNORE_1\t$ids[1]\tIgnored longline of something\n";
		}
		else
		{
			print TEST join ("\t", @ids, $data_str), "\n";
			my @sorted_ids = sort(@ids);
			if ($sorted_ids[0] =~ /ID_A/ && 
				$sorted_ids[1] =~ /ID_B/)
			{
				print CORRECT_AB join ("\t", @ids, $data_str), "\n";
			}
			elsif ($sorted_ids[0] =~ /ID_A/ && 
				$sorted_ids[1] =~ /ID_A/)
			{
				print CORRECT_A join ("\t", @ids, $data_str), "\n";
			}
			else
			{
				print CORRECT_B join ("\t", @ids, $data_str), "\n";
			}
		}
	}
	
	close TEST;
	close CORRECT;
}
system ('seq_pairs_filter_by_regex -i test.data --verbose --verbose --regex "ID_A" --alt_regex "ID_B" > compare_ab.data');
system ('seq_pairs_filter_by_regex -i test.data --verbose --verbose --regex "ID_A" > compare_a.data');
system ('seq_pairs_filter_by_regex -i test.data --verbose --verbose --regex "ID_B" > compare_b.data');
print STDERR "\tComparing results...\n";
{
	for ("_a", "_ab", "_b")
	{
		my @diff = `diff compare$_.data correct$_.data`;
		if (@diff != 0)
		{
			print STDERR "FAILED: compare$_.data correct$_.data are not the same!\n";
			exit;
		}
	}
}
print STDERR "\tCleaning up...\n";
unlink (glob("*.data"));
print STDERR "\n\tSUCCESS!!!\n\n";
